//
//  ModifyPhoneViewController.m
//  tuanGG
//
//  Created by 微他 on 15/12/3.
//  Copyright © 2015年 weita. All rights reserved.
//

#import "ModifyPhoneViewController.h"

@interface ModifyPhoneViewController ()
@property(nonatomic,strong)UITextField *phoneTextField;
@property(nonatomic,strong)UITextField *VerificationTextField;
@property(nonatomic,strong)UIButton *Verification;
@property(nonatomic,strong)UIButton *saveButton;
@end

@implementation ModifyPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    [self addnavigationTitle:@"修改绑定手机" navColor:@"4fd2be"];
    [self initView];
}
- (void)initView {
    _phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 50)];
    _phoneTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _phoneTextField.leftViewMode =  UITextFieldViewModeAlways;
    _phoneTextField.placeholder =@"请输入手机号码";
    _phoneTextField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_phoneTextField];

    UIView *line  = [[UIView alloc]initWithFrame:CGRectMake( 0,CGRectGetMaxY(_phoneTextField.frame) , ScreenWidth, 1)];
    line.backgroundColor = [UIColor blackColor];
    line.alpha = 0.1;
    [self.view addSubview:line];

    _VerificationTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_phoneTextField.frame)+1, ScreenWidth, 50)];
    _VerificationTextField.placeholder = @"请输入验证码";
    _VerificationTextField.backgroundColor = [UIColor whiteColor];
    _VerificationTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _VerificationTextField.leftViewMode =  UITextFieldViewModeAlways;
    [self.view addSubview:_VerificationTextField];

    _Verification = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-70, _phoneTextField.frame.size.height/2+5, 60, 30)];
    [_Verification setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_Verification setTitleColor:[UIColor colorWithHexString:@"33ccbf"] forState:UIControlStateNormal];
    [_Verification addTarget:self action:@selector(verificationCode) forControlEvents:UIControlEventTouchUpInside];
    _Verification.titleLabel.numberOfLines = 2;
    _Verification.layer.cornerRadius = 5;
    _Verification.layer.borderWidth = 1;
    _Verification.layer.borderColor = [[UIColor colorWithHexString:@"33ccbf"]CGColor];
    _Verification.titleLabel.font = BOLD_FONT(11);
    [self.view addSubview:_Verification];

    _saveButton = [[UIButton alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_VerificationTextField.frame)+30, ScreenWidth-20, 40)];
    [_saveButton setTitle:@"确定" forState:UIControlStateNormal];
    [_saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _saveButton.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    _saveButton.layer.cornerRadius = 5;
    [_saveButton addTarget:self action:@selector(saveButtonBuild:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_saveButton];

}
- (void)saveButtonBuild:(UITextField *)sender {
        if (_phoneTextField.text.length == 0) {
            [Common addAlert:@"手机号不能为空"];
        }else if (_VerificationTextField.text.length != 4) {
            [Common addAlert:@"验证码不正确"];
        }else{
            [self Networking];
        }

}
- (void)verificationCode{
    if ([_phoneTextField.text isEqualToString:@""]) {
        [Common addAlert:@"请输入您的手机号码"];

    }
    else
    {
        if (![ValidateMobileWithEmail validateMobile:_phoneTextField.text]) {
            [Common addAlert:@"手机号码格式不正确"];
        }
        else
        {
            [CustomAlertView alterMsg:[NSString stringWithFormat:@"是否是通过%@号码获取验证码吗?",self.phoneTextField.text] delegate:self];
        }

    }
}
#pragma amrk - <UIAlertViewDelegate>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"sssssss");
        //获取验证码
        [Common verificationCode:^{
            [_Verification setTitle:@"获取验证码" forState:UIControlStateNormal];
            _Verification.userInteractionEnabled = YES;
        } blockNo:^(id time) {
            [_Verification setTitle:[NSString stringWithFormat:@"获取验证码\n     %@'s",time] forState:UIControlStateNormal];
            NSLog(@"%@",_Verification.titleLabel.text);
            _Verification.userInteractionEnabled = NO;
            //发送验证
        }];

        //发送验证信息
//        [SMS_SDK getVerificationCodeBySMSWithPhone:self.phoneTextField.text zone:@"86"   result:^(SMS_SDKError *error) {
//
//            if (!error) {
//
//                NSLog(@"");
//            }
//            else
//            {   if (!error.description) {
//                NSLog(@"错误:%@",error.description);
//            }
//                [CustomAlertView alterSureMsg:[NSString stringWithFormat:@"%@",error.description] delegate:nil];
//            }
//
//        }];
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_phoneTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
            if (!error) {
                [Common addAlert:@"发送成功"];
            }else {
                [Common addAlert:@"发送失败"];
            }
        }];

    }
}
- (void)Networking {
    NSDictionary *dictionary = @{@"newphone":_phoneTextField.text,@"vcode":_VerificationTextField.text};
    [GLAFRequest startRequest:alterMydataPhoneold_url paramsDic:dictionary defaultdialog:YES ispost:YES handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue] == 1) {
            [Common addAlert:@"修改成功"];
            [self.navigationController popViewControllerAnimated:YES];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } doFinally:^(NSDictionary *paramsDic) {

    }];
}
@end
